package com.xlong.component.task;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

import com.alibaba.fastjson.JSONObject;
import com.xlong.BeanEntity.ResponseMsg;
import com.xlong.BeanEntity.TaskEntity;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class TaskQueue {
	private final int QUEUE_LENGTH = 2000;
	private static TaskQueue INSTANCE = new TaskQueue();
    private BlockingQueue<TaskEntity> queue = new LinkedBlockingDeque<>(QUEUE_LENGTH);
    
    

    private String taskId;
    
    public static TaskQueue getInstance() {
    	return INSTANCE;
    }
    
    //加入任务
    public void put(DeferredResult<ResponseMsg<JSONObject>> deferredResult,JSONObject map){
    	taskId = UUID.randomUUID().toString();
        log.info("任务加入队列，id为：{}",taskId);
        queue.offer(new TaskEntity(taskId,deferredResult,map));
    }

    //获取任务
    public TaskEntity take() throws InterruptedException {

    	TaskEntity task = queue.poll();

        if (task != null) {
            log.info("获得任务:{}", task.getTaskId());
        }
        return task;

    }
}
